package com.mifengs.mall.widget;

import android.content.Context;
import android.content.res.Resources;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.mifengs.mall.R;
import com.mifengs.mall.util.UIUtil;

/**
 * Created by lihaoqi on 2018/4/23.
 */

public class HomeViewBehavior extends CoordinatorLayout.Behavior<LinearLayout> {


    private Context mContext;
    private ImageView mToSignView;
    private ImageView mToTopView;
    private int mLLWidth;

    /**
     * 因为是在XML中使用app:layout_behavior定义静态的这种行为,
     * 必须实现一个构造函数使布局的效果能够正常工作。
     * 否则 Could not inflate Behavior subclass error messages.
     *
     * @param context
     * @param attrs
     */
    public HomeViewBehavior(Context context, AttributeSet attrs) {
        super();
        this.mContext = context;
    }

    /**
     * 处理垂直方向上的滚动事件
     *
     * @param coordinatorLayout
     * @param child
     * @param directTargetChild
     * @param target
     * @param nestedScrollAxes
     * @return
     */
    @Override
    public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout,
                                       LinearLayout child, View directTargetChild, View target, int nestedScrollAxes) {

        // Ensure we react to vertical scrolling
        return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL ||
                super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target,
                        nestedScrollAxes);
    }


    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, LinearLayout child, View dependency) {
        if(child.getId()==R.id.ll_float&&(mToSignView ==null)){
            mToSignView = child.findViewById(R.id.iv_to_sign);
            mToTopView =  child.findViewById(R.id.iv_to_top);
        }
        return dependency instanceof RecyclerView;
    }



    int offset;

    @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, final LinearLayout child, View dependency) {
        Resources resources = mContext.getResources();
        DisplayMetrics dm =resources.getDisplayMetrics();
        final int height = dm.heightPixels;
        final RecyclerView rvList = (RecyclerView) dependency;
        if(mLLWidth==0){
            mLLWidth = child.getMeasuredWidth();
        }
        rvList.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                offset = rvList.computeVerticalScrollOffset();
                //LogUtil.i("offset",":"+offset);
                if(offset>(1.3f*height)){
                    if(mToTopView.getVisibility() == View.INVISIBLE)
                        mToTopView.setVisibility(View.VISIBLE);
                    //toTop(child);
                }else if (offset<(1.1f*height)){
                    if(mToTopView.getVisibility() == View.VISIBLE)
                        mToTopView.setVisibility(View.INVISIBLE);
                    //toBottom(child);
                }
            }

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                switch (newState){
                    case RecyclerView.SCROLL_STATE_IDLE:
                        toLeft(child);
                        if(offset>(1.2f*height)){
                            if(mToTopView.getVisibility() == View.INVISIBLE)
                                mToTopView.setVisibility(View.VISIBLE);
                        }else if (offset<(1.1f*height)){
                            if(mToTopView.getVisibility() == View.VISIBLE)
                                mToTopView.setVisibility(View.INVISIBLE);
                        }
                        break;
                    case RecyclerView.SCROLL_STATE_DRAGGING:
                        toRight(child);
                        break;
                    case RecyclerView.SCROLL_STATE_SETTLING:
                        break;
                }
            }
        });

        return true;
    }

    /**
     * 检查Y的位置，并决定按钮是否动画进入或退出
     *
     * @param coordinatorLayout
     * @param child
     * @param target
     * @param dxConsumed
     * @param dyConsumed
     * @param dxUnconsumed
     * @param dyUnconsumed
     */
    @Override
    public void onNestedScroll(CoordinatorLayout coordinatorLayout, LinearLayout child,
                               View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
        super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed,
                dyUnconsumed);
    }



    private void toRight(final View view){
        view.animate().cancel();
        view.animate()
                .translationX(mLLWidth/2)
                .alpha(0.5f)
                .setDuration(200)
                .setInterpolator(new LinearInterpolator());
    }


    private void toLeft(final View view){
        view.animate().cancel();
        view.animate()
                .translationX(0)
                .alpha(1f)
                .setDuration(200)
                .setInterpolator(new LinearInterpolator());
    }


    private void toTop(final View view){
        //view.animate().cancel();
        view.animate()
                .translationY(-UIUtil.dp2px(66))
                .setDuration(200)
                .setInterpolator(new LinearInterpolator());
    }


    private void toBottom(final View view){
        //view.animate().cancel();
        view.animate()
                .translationY(0)
                .setDuration(200)
                .setInterpolator(new LinearInterpolator());
    }

}
